package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.grid.RowExpander;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.newtype.meeting.shared.MeetingNotifyDTO;

/**
 * 点名表
 * @author 汪恭泽
 * Jun 29, 2011 9:57:40 AM
 */
public class NamedTableWindow extends Window {
	private TabPanel panel;
	private ContentPanel cp;

	GroupingStore<MeetingNotifyDTO> subscribeStore = new GroupingStore<MeetingNotifyDTO>();//预订出席
	GroupingStore<MeetingNotifyDTO> incapableStore = new GroupingStore<MeetingNotifyDTO>();//无法出席
	GroupingStore<MeetingNotifyDTO> notyetStore = new GroupingStore<MeetingNotifyDTO>();//尚未回复

	public NamedTableWindow() {
		setSize(508, 518);
		//setStyleAttribute("background", "white");

		panel = new TabPanel();
		panel.setPlain(true);
		panel.setAutoHeight(true);
		panel.setStyleAttribute("padding", "5px");

		createItem1();//预定出席成员
		createItem2();//无法出席成员
		createItem3();//尚未回复

		add(panel);
	}

	//尚未回复
	private void createItem3() {
		TabItem item = new TabItem("显示尚未回复");
		cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setStyleAttribute("padding", "5px");
		cp.setAutoHeight(true);
		cp.setLayout(new FlowLayout());

		createTitle("[ 尚未回复 ]");

		notyetStore.groupBy("deptName");
		ColumnConfig fullName = new ColumnConfig("fullName", "姓名", 250);
		ColumnConfig deptName = new ColumnConfig("deptName", "部门", 250);

		CheckColumnConfig checkAttend = new CheckColumnConfig("attendedTAG",
				"是否出席", 150);
		checkAttend.setAlignment(HorizontalAlignment.CENTER);
		CellEditor checkBoxEditor = new CellEditor(new CheckBox());
		checkAttend.setEditor(checkBoxEditor);

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		configs.add(fullName);
		configs.add(deptName);
		configs.add(checkAttend);

		final ColumnModel cm = new ColumnModel(configs);

		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setGroupRenderer(new GridGroupRenderer() {
			@Override
			public String render(GroupColumnData data) {
				return data.group;
			}
		});

		Grid<MeetingNotifyDTO> grid = new Grid<MeetingNotifyDTO>(notyetStore,
				cm);
		grid.setStyleAttribute("padding", "5px");
		grid.addPlugin(checkAttend);
		grid.setView(view);
		grid.setBorders(false);
		grid.setAutoHeight(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		cp.add(grid);

		item.add(cp);
		panel.add(item);
	}

	//无法出席成员
	private void createItem2() {
		TabItem item = new TabItem("显示无法出席成员");
		cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setStyleAttribute("padding", "5px");
		cp.setAutoHeight(true);
		cp.setLayout(new FlowLayout());

		createTitle("[ 无法出席成员 ]");

		XTemplate tpl = XTemplate
				.create("<p style='color:red;'>[ 回复内容 ]</p><p>&nbsp;&nbsp;&nbsp;&nbsp;{replyContent}</p>");
		RowExpander expander = new RowExpander();
		expander.setTemplate(tpl);

		incapableStore.groupBy("deptName");
		ColumnConfig fullName = new ColumnConfig("fullName", "姓名", 250);
		ColumnConfig deptName = new ColumnConfig("deptName", "部门", 250);

		CheckColumnConfig checkAttend = new CheckColumnConfig("attendedTAG",
				"是否出席", 150);
		checkAttend.setAlignment(HorizontalAlignment.CENTER);
		CellEditor checkBoxEditor = new CellEditor(new CheckBox());
		checkAttend.setEditor(checkBoxEditor);

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		configs.add(expander);
		configs.add(fullName);
		configs.add(deptName);
		configs.add(checkAttend);

		final ColumnModel cm = new ColumnModel(configs);

		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setGroupRenderer(new GridGroupRenderer() {
			@Override
			public String render(GroupColumnData data) {
				return data.group;
			}
		});

		Grid<MeetingNotifyDTO> grid = new Grid<MeetingNotifyDTO>(
				incapableStore, cm);
		grid.setStyleAttribute("padding", "5px");
		grid.addPlugin(expander);
		grid.addPlugin(checkAttend);
		grid.setView(view);
		grid.setBorders(false);
		grid.setAutoHeight(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		cp.add(grid);

		item.add(cp);
		panel.add(item);
	}

	//预定出席成员
	private void createItem1() {
		TabItem item = new TabItem("显示预定出席成员");
		cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setStyleAttribute("padding", "5px");
		cp.setAutoHeight(true);
		cp.setLayout(new FlowLayout());

		createTitle("[ 预定出席成员 ]");

		XTemplate tpl = XTemplate
				.create("<p style='color:red;'>[ 回复内容 ]</p><p>&nbsp;&nbsp;&nbsp;&nbsp;{replyContent}</p>");
		RowExpander expander = new RowExpander();
		expander.setTemplate(tpl);

		subscribeStore.groupBy("deptName");
		ColumnConfig fullName = new ColumnConfig("fullName", "姓名", 250);
		ColumnConfig deptName = new ColumnConfig("deptName", "部门", 250);

		CheckColumnConfig checkAttend = new CheckColumnConfig("attendedTAG",
				"是否出席", 150);
		checkAttend.setAlignment(HorizontalAlignment.CENTER);
		CellEditor checkBoxEditor = new CellEditor(new CheckBox());
		checkAttend.setEditor(checkBoxEditor);

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		configs.add(expander);
		configs.add(fullName);
		configs.add(deptName);
		configs.add(checkAttend);

		final ColumnModel cm = new ColumnModel(configs);

		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setGroupRenderer(new GridGroupRenderer() {
			@Override
			public String render(GroupColumnData data) {
				return data.group;
			}
		});

		Grid<MeetingNotifyDTO> grid = new Grid<MeetingNotifyDTO>(
				subscribeStore, cm);
		grid.setStyleAttribute("padding", "5px");
		grid.addPlugin(expander);
		grid.addPlugin(checkAttend);
		grid.setView(view);
		grid.setBorders(false);
		grid.setAutoHeight(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		cp.add(grid);

		item.add(cp);
		panel.add(item);
	}

	private void createTitle(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb.append("<p style='font-size:18px;font-weight: bold;color:#0060A0;'>"
				+ str + "</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		title.setStyleAttribute("background", "white");
		title.setStyleAttribute("padding", "5px");
		cp.add(title);
	}

	public GroupingStore<MeetingNotifyDTO> getSubscribeStore() {
		return subscribeStore;
	}

	public GroupingStore<MeetingNotifyDTO> getIncapableStore() {
		return incapableStore;
	}

	public GroupingStore<MeetingNotifyDTO> getNotyetStore() {
		return notyetStore;
	}
}
